<template>
  <!-- 对话信息 -->
  <div class="messages-area" ref="messagesArea">
    <div v-for="(msg, idx) in messages" :key="msg.messageId || idx" :class="['message', msg.role]"
      :style="{ animationDelay: idx * 0.1 + 's' }">
      <div class="message-content">{{ msg.content }}</div>
      
      <!-- 语音控制区域 -->
      <div v-if="msg.role === 'assistant' && msg.hasAudio" class="audio-controls">
        <!-- 正在播放状态 -->
        <div v-if="msg.isPlaying" class="voice-indicator playing">
          <i class="fas fa-volume-up"></i>
          <span>正在播放语音...</span>
          <div class="wave-animation">
            <span></span>
            <span></span>
            <span></span>
          </div>
        </div>
        
        <!-- 可重播状态 -->
        <div v-else class="voice-controls">
          <button @click="$emit('replay-audio', msg.messageId)" class="replay-btn">
            <i class="fas fa-play"></i>
            <span>重播语音</span>
          </button>
          <span class="audio-available">
            <i class="fas fa-check-circle"></i>
            语音可用
          </span>
        </div>
      </div>
      
      <div class="message-time">{{ formatTime(msg.timestamp) }}</div>
    </div>
    
    <div v-if="messages.length === 0" class="message system">
      <div class="message-content">
        你好！我是{{ character.name }}。{{ character.greeting }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick, defineProps, defineEmits, watch } from 'vue';

const props = defineProps({
  messages: Array,
  character: Object
});

const emit = defineEmits(['replay-audio']);

const messagesArea = ref(null);

const formatTime = (timestamp) => timestamp.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });

// 监听消息变化，自动滚动到底部
watch(() => props.messages, () => {
  nextTick(() => {
    if (messagesArea.value) {
      messagesArea.value.scrollTop = messagesArea.value.scrollHeight;
    }
  });
}, { deep: true });

onMounted(() => {
  nextTick(() => {
    if (messagesArea.value) messagesArea.value.scrollTop = messagesArea.value.scrollHeight;
  });
});
</script>

<style scoped>
.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.messages-area::-webkit-scrollbar {
  width: 6px;
}

.messages-area::-webkit-scrollbar-track {
  background: transparent;
}

.messages-area::-webkit-scrollbar-thumb {
  background: var(--primary);
  border-radius: 3px;
}

/* 消息样式 */
.message {
  padding: 15px 20px;
  border-radius: 20px;
  max-width: 80%;
  position: relative;
  opacity: 0;
  transform: translateY(20px);
  animation: fadeUp 0.4s forwards;
  transition: var(--transition);
  backdrop-filter: blur(10px);
  border: 1px solid var(--glass-border);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

@keyframes fadeUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message.user {
  background: linear-gradient(45deg, rgba(74, 108, 247, 0.3), rgba(59, 89, 214, 0.2));
  align-self: flex-end;
  border-top-right-radius: 5px;
}

.message.assistant {
  background: linear-gradient(45deg, rgba(169, 202, 255, 0.8), rgba(18, 66, 177, 0.6));
  align-self: flex-start;
  border-top-left-radius: 5px;
}

.message.system {
  background: linear-gradient(45deg, rgba(254, 202, 87, 0.3), rgba(15, 23, 42, 0.3));
  align-self: center;
  text-align: center;
  max-width: 70%;
  font-size: 0.9rem;
}

.message-content {
  font-size: 1rem;
  line-height: 1.5;
  position: relative;
  z-index: 2;
}

.message-time {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 5px;
  text-align: right;
}

/* 音频控制区域 */
.audio-controls {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* 正在播放指示器 */
.voice-indicator.playing {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: rgba(245, 158, 11, 0.2);
  border-radius: 15px;
  font-size: 0.8rem;
  color: #f59e0b;
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.voice-indicator.playing i {
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1); opacity: 0.8; }
}

/* 音波动画 */
.wave-animation {
  display: flex;
  gap: 2px;
  align-items: center;
}

.wave-animation span {
  width: 2px;
  height: 8px;
  background: #f59e0b;
  border-radius: 1px;
  animation: wave 1.4s infinite ease-in-out;
}

.wave-animation span:nth-child(1) { animation-delay: -1.1s; }
.wave-animation span:nth-child(2) { animation-delay: -1.0s; }
.wave-animation span:nth-child(3) { animation-delay: -0.9s; }

@keyframes wave {
  0%, 40%, 100% { transform: scaleY(0.4); }
  20% { transform: scaleY(1); }
}

/* 语音控制按钮区域 */
.voice-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.replay-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(74, 108, 247, 0.3);
  color: #4a6cf7;
  border: 1px solid rgba(74, 108, 247, 0.5);
  padding: 6px 12px;
  border-radius: 12px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(5px);
}

.replay-btn:hover {
  background: rgba(74, 108, 247, 0.4);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(74, 108, 247, 0.3);
}

.replay-btn:active {
  transform: translateY(0);
}

.audio-available {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.7rem;
  color: #10b981;
  opacity: 0.8;
}

.audio-available i {
  font-size: 0.7rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .message {
    max-width: 90%;
    padding: 12px 15px;
  }
  
  .voice-controls {
    flex-direction: column;
    align-items: flex-start;
    gap: 5px;
  }
  
  .replay-btn {
    padding: 4px 8px;
    font-size: 0.7rem;
  }
}
</style>

